package com.sf.dlb.web.service;

import com.sf.dlb.bean.UserGradeBean;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.jpa.model.UserGradeInfo;
import com.sf.dlb.jpa.repository.UserGradeRepository;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by tanjing on 2019/5/7.
 */
@Service
public class WUserGradeService {
    private Logger logger= LoggerFactory.getLogger(WUserGradeService.class);
    @Autowired
    private UserGradeRepository userGradeRepository;
    /**
     * 等级信息列表
     * @return
     */
    public ResultVO list(){
        logger.info("==list start");
        UserGradeInfo userGradeInfo=new UserGradeInfo();
        userGradeInfo.setDelFlag("0");
        Example example=Example.of(userGradeInfo);
        Sort sort=new Sort(Sort.Direction.DESC,"createDate");
        List<UserGradeInfo> userMessageInfos = userGradeRepository.findAll(example,sort);
        return ResultVOUtil.success(userMessageInfos);
    }
    /**
     * 添加等级信息
     */
    @Transactional
    public ResultVO add(UserGradeBean userGradeBean){
        logger.info("==add start userGradeBean="+ GsonUtils.getInstance().toJson(userGradeBean));
        UserGradeInfo userGradeInfo=new UserGradeInfo();
        BeanUtils.copyProperties(userGradeBean,userGradeInfo);
        userGradeInfo.setCreateDate(new Date());
        userGradeInfo.setUpdateDate(new Date());
        userGradeRepository.save(userGradeInfo);
        return ResultVOUtil.success();
    }
    /**
     * 修改等级信息
     *
     */
    @Transactional
    public ResultVO update(UserGradeBean userGradeBean){
        logger.info("==update start userGradeBean="+ GsonUtils.getInstance().toJson(userGradeBean));
        UserGradeInfo userGradeInfo=userGradeRepository.findById(userGradeBean.getId());
        if(null==userGradeInfo){
            return ResultVOUtil.error(ResponseConstant.ERROR,"等级信息不存在");
        }
        if(StringUtils.isNotBlank(userGradeBean.getGrade())){
            userGradeInfo.setGrade(userGradeBean.getGrade());
        }
        if(StringUtils.isNotBlank(userGradeBean.getName())){
            userGradeInfo.setName(userGradeBean.getName());
        }
        if(StringUtils.isNotBlank(userGradeBean.getGradeDesc())){
            userGradeInfo.setGradeDesc(userGradeBean.getGradeDesc());
        }
        if(null!=userGradeBean.getPrice()){
            userGradeInfo.setPrice(userGradeBean.getPrice());
        }
        if(null!=userGradeBean.getValidity()){
            userGradeInfo.setValidity(userGradeBean.getValidity());
        }
        if(null!=userGradeBean.getMultiple()){
            userGradeInfo.setMultiple(userGradeBean.getMultiple());
        }
        if(null!=userGradeBean.getInvitationNumber()){
            userGradeInfo.setInvitationNumber(userGradeBean.getInvitationNumber());
        }
        if(null!=userGradeBean.getReadDivide()){
            userGradeInfo.setReadDivide(userGradeBean.getReadDivide());
        }
        if(null!=userGradeBean.getBuyDivide()){
            userGradeInfo.setBuyDivide(userGradeBean.getBuyDivide());
        }
        if(null!=userGradeBean.getMoneyPoomDivide()){
            userGradeInfo.setMoneyPoomDivide(userGradeBean.getMoneyPoomDivide());
        }
        userGradeInfo.setUpdateDate(new Date());
        userGradeRepository.save(userGradeInfo);
        return ResultVOUtil.success();
    }
    /**
     * 删除等级信息
     *
     */
    @Transactional
    public ResultVO delete(Long id){
        logger.info("==delete id={} ",id);
        UserGradeInfo userGradeInfo=userGradeRepository.findById(id);
        if(null==userGradeInfo){
            return ResultVOUtil.error(ResponseConstant.ERROR,"等级信息不存在");
        }
        userGradeInfo.setDelFlag("1");
        userGradeInfo.setUpdateDate(new Date());
        userGradeRepository.save(userGradeInfo);
        return ResultVOUtil.success();
    }

}
